package com.zsgeek.wisdchain.qutu.view.home;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.zsgeek.wisdchain.qutu.R;
import com.zsgeek.wisdchain.qutu.base.http.JsonRequest;
import com.zsgeek.wisdchain.qutu.base.http.JsonRequestCallBack;
import com.zsgeek.wisdchain.qutu.base.model.BannerItemDto;
import com.zsgeek.wisdchain.qutu.base.model.QueryUserModel;
import com.zsgeek.wisdchain.qutu.tools.ImageUtils;
import com.zsgeek.wisdchain.qutu.tools.TextUtils;
import com.zsgeek.wisdchain.qutu.tools.ToastHelper;
import com.zsgeek.wisdchain.qutu.tools.UrlPathHelper;
import com.zsgeek.wisdchain.qutu.view.BaseFragment;
import com.zsgeek.wisdchain.qutu.view.MainActivity;
import com.zsgeek.wisdchain.qutu.view.me.MyCommentActivity;
import com.zsgeek.wisdchain.qutu.view.me.MyConcernActivity;
import com.zsgeek.wisdchain.qutu.view.me.ViewProfileActivity;
import com.zsgeek.wisdchain.qutu.view.me.adapter.LoopViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的
 * <p/>
 */
public class MeFragment extends BaseFragment {
    @BindView(R.id.nick_name_tv)
    TextView nick_name_tv;
    @BindView(R.id.ad_list_tv)
    TextView ad_list_tv;
    @BindView(R.id.data_statistics_tv)
    TextView data_statistics_tv;
    @BindView(R.id.user_code_tv)
    TextView user_code_tv;
    @BindView(R.id.avatar_iv)
    ImageView avatar_iv;
//    @BindView(R.id.home_vp_container)
//    ViewPager home_vp_container;
//    @BindView(R.id.home_ll_indicators)
//    LinearLayout homeLlIndicators;

    private LoopViewPagerAdapter loopViewPagerAdapter;
    private List<BannerItemDto> adsList = new ArrayList<BannerItemDto>();

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_me;
    }

    @Override
    protected void initView(View view) {
//        initViewPager();
    }

    @Override
    protected void initData() {
        if (!TextUtils.isEmpty(MainActivity.address)) {
            if (MainActivity.userDataBean == null) {
                getQueryUser(MainActivity.address);
            } else {
                setView();
            }
        }
//        getTopBanner();
    }

    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    @OnClick({R.id.view_profile_rl, R.id.my_concern_rl, R.id.my_comments_rl, R.id.my_praise_rl,
            R.id.history_browsing_rl, R.id.face_red_envelope_fl, R.id.friends_and_relatives_fl, R.id.invite_friends_fl, R.id.small_game_class_fl})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.view_profile_rl:  //查看个人主页
                if (!TextUtils.isEmpty(MainActivity.address)) {
                    if (MainActivity.userDataBean != null) {
                        startActivity(new Intent(getActivity(), ViewProfileActivity.class));
                    }
                }
                break;
            case R.id.my_concern_rl:
                startNewActivity(0, MyConcernActivity.class);
                break;
            case R.id.my_comments_rl:
                startNewActivity(1, MyCommentActivity.class);
                break;
            case R.id.my_praise_rl:
                startNewActivity(2, MyCommentActivity.class);
                break;
            case R.id.history_browsing_rl:
                startNewActivity(3, MyCommentActivity.class);
                break;
            case R.id.face_red_envelope_fl:
                startActivity(new Intent(getActivity(), BaseWebViewActivity.class).putExtra("title", getString(R.string.mdmhb))
                        .putExtra("path", "https://game.gamec.biz/paopaolong/regs.htm?coinAddress=" + MainActivity.address));
                break;
            case R.id.friends_and_relatives_fl:
                startActivity(new Intent(getActivity(), BaseWebViewActivity.class).putExtra("title", getString(R.string.qyhb))
                        .putExtra("path", "https://game.gamec.biz/paopaolong/regs.htm?coinAddress=" + MainActivity.address));
                break;
            case R.id.invite_friends_fl:
                startActivity(new Intent(getActivity(), BaseWebViewActivity.class).putExtra("title", getString(R.string.yqhy))
                        .putExtra("path", "https://game.gamec.biz/paopaolong/regs.htm?coinAddress=" + MainActivity.address));
                break;
            case R.id.small_game_class_fl:
                startActivity(new Intent(getActivity(), BaseWebViewActivity.class).putExtra("title", getString(R.string.xyxl))
                        .putExtra("path", "https://game.gamec.biz/paopaolong/regs.htm?coinAddress=" + MainActivity.address));
                break;
        }
    }

    private void startNewActivity(int type, Class<?> cls) {
        startActivity(new Intent(getActivity(), cls).putExtra("type", type));
    }

//    private void initViewPager() {
//        loopViewPagerAdapter = new LoopViewPagerAdapter(getActivity(), home_vp_container, homeLlIndicators);
//        home_vp_container.setAdapter(loopViewPagerAdapter);
//        loopViewPagerAdapter.setList(adsList);
//        home_vp_container.addOnPageChangeListener(loopViewPagerAdapter);
//
////        mConsumeGoodsAapter = new ConsumeGoodsAapter(getActivity(), brandList);
////        consume_goods_gv.setAdapter(mConsumeGoodsAapter);
//    }
//
//    private void getTopBanner() {
////        DataManager.getInstance().getBannerList(new DefaultSingleObserver<HttpResult<BannerInfoDto>>() {
////            @Override
////            public void onSuccess(HttpResult<BannerInfoDto> result) {
////                if (result != null) {
////                    if (result.getData() != null) {
////                        List<BannerItemDto> bannerList = result.getData().getIndex_top();
//        adsList.add(new BannerItemDto("http://img1.imgtn.bdimg.com/it/u=2174909441,2495215020&fm=26&gp=0.jpg"));
//        adsList.add(new BannerItemDto("http://img0.imgtn.bdimg.com/it/u=1551039200,1108067788&fm=26&gp=0.jpg"));
//        adsList.add(new BannerItemDto("http://img4.imgtn.bdimg.com/it/u=4105564734,2977816416&fm=214&gp=0.jpg"));
//        loopViewPagerAdapter.setList(adsList);
////                    }
////                }
////            }
////
////            @Override
////            public void onError(Throwable throwable) {
////            }
////        }, "index_top");
//    }

    private void setView() {
        QueryUserModel.DataBean userDataBean = MainActivity.userDataBean;
        ad_list_tv.setText(String.valueOf(userDataBean.getAdverNum()));
        data_statistics_tv.setText(String.valueOf(userDataBean.getDataNum()));
        QueryUserModel.DataBean.UserBean userBean = userDataBean.getUser();
        if (userBean != null) {
            nick_name_tv.setText(userBean.getNickName());
            user_code_tv.setText(userBean.getUserCode());
            ImageUtils.loadImage(getActivity(), userBean.getHeadImg(), avatar_iv);
        }
    }

    /**
     * 获取个人资料
     *
     * @param address
     */
    private void getQueryUser(String address) {
        new JsonRequest(new JsonRequestCallBack() {
            @Override
            public void showResult(Object object, String taskId) {
                if (object instanceof QueryUserModel) {
                    QueryUserModel dataModel = (QueryUserModel) object;
                    if (dataModel.getCode() == 2000) {
                        MainActivity.userDataBean = dataModel.getData();
                        setView();
                    } else ToastHelper.showToast(dataModel.getMessage());
                }
            }

            @Override
            public void showError(String errorMsg, String taskId) {
                ToastHelper.showToast(errorMsg);
            }
        }).postJson(UrlPathHelper.getAdver() + "user/queryUser?addr=" + address, true, null, QueryUserModel.class);
    }

}
